<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use App\Models\StoreWithDrawal;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class StoreWithdrawalController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '门店提现申请';
    
    private $status = [
    	1 => '申请中',
	    2 => '同意',
	    3 => '拒绝'
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new StoreWithDrawal());
        
        $grid->column('price', '金额')->sortable();
        $grid->column('applicant.name', '申请人');
        $grid->column('admin_id', '同意人');
        $grid->column('remark', __('Remark'));
        $grid->column('status', __('Status'))->using($this->status);
        $grid->column('created_at', __('Created at'))->sortable();
        
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->equal('applicant_id', '申请人')->select(AdminUser::where('store_id', Admin::user()->store_id)->get()->pluck('name', 'id'));
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(StoreWithDrawal::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new StoreWithDrawal());

        $form->hidden('store_id', __('Store id'))->default(Admin::user()->store_id);
        $form->currency('price', '金额')->symbol('￥')->required();
        $form->hidden('applicant_id', __('Applicant id'))->default(Admin::user()->id);
        $form->hidden('admin_id', __('Admin id'));
        $form->textarea('remark', '申请理由或备注')->rules('max:190');
        $form->hidden('status')->default(1);
        
        if ($form->isEditing()) {
            $form->radio('status', __('Status'))
	            ->options($this->status)
	            ->default(1)
	            ->disable();
            
        }
        
        return $form;
    }
}
